package com.controller;

import com.POJO.GradeRequest;
import com.POJO.User;
import com.service.GradeRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Package com.controller
 * @Date 2024-05-23 12:12
 * @Description 成绩修改申请相关接口，前端对应teacherReviewRequests和studentGradeRequest
 */
@Controller
@RequestMapping("/gradeRequest")
public class GradeRequestController {

    @Autowired
    private GradeRequestService gradeRequestService;

    /**
     * 学生提出成绩修改申请
     * @param gradeRequest 成绩修改申请对象
     * @return 返回操作结果视图
     */
    @PostMapping("/add")
    public ModelAndView addGradeRequest(@ModelAttribute("gradeRequest") GradeRequest gradeRequest, @SessionAttribute("user") User user) {
        gradeRequestService.createGradeRequest(
                user.getUserId(),
                gradeRequest.getSubjectId(),
                gradeRequest.getReason(),
                gradeRequest.getRequestedGrade(),
                "pending"
        );
        int id = gradeRequest.getStudentId();
//        System.out.println("Student ID: " + id);
        return new ModelAndView("redirect:/gradeRequest/student/" + id);
    }

    /**
     * 获取学生的所有成绩修改申请
     * @param studentId 学生ID
     * @return 返回学生的成绩修改申请视图
     */
    @GetMapping("/student/{studentId}")
    public ModelAndView getGradeRequestsByStudentId(@PathVariable("studentId") int studentId) {
        List<GradeRequest> gradeRequests = gradeRequestService.getGradeRequestsByStudentId(studentId);
        ModelAndView mv = new ModelAndView("studentGradeRequest");
        mv.addObject("gradeRequests", gradeRequests);
        return mv;
    }

    /**
     * 跳转到学生成绩申请请求页面
     * @return 页面视图
     */
    @GetMapping("/student")
    public String toStudentGradeRequest(){
        return "studentGradeRequest";
    }

    /**
     * 获取教师待审核的所有成绩修改申请
     * @param teacherId 教师ID
     * @return 返回教师待审核的成绩修改申请视图
     */
    @GetMapping("/teacher/{teacherId}")
    public ModelAndView getPendingGradeRequestsByTeacherId(@PathVariable("teacherId") int teacherId, HttpSession session) {
        List<GradeRequest> pendingRequests = gradeRequestService.getPendingGradeRequestsByTeacher(teacherId);
        session.setAttribute("teacherId", teacherId); // 将教师ID放入Session
        ModelAndView mv = new ModelAndView("teacherReviewRequests");
        mv.addObject("pendingRequests", pendingRequests);
        return mv;
    }

    /**
     * 获取教师待处理的成绩修改申请列表
     * @param teacherId 教师ID
     * @return 待处理申请列表
     */
    @GetMapping("/getPendingRequests/{teacherId}")
    @ResponseBody
    public List<GradeRequest> getPendingRequests(@PathVariable("teacherId") int teacherId) {
        return gradeRequestService.getPendingGradeRequestsByTeacher(teacherId);
    }

    /**
     * 教师审核成绩修改申请
     * @param gradeRequest 成绩修改申请对象
     * @return 返回审核结果
     */
    @PostMapping("/review")
    @ResponseBody
    public String reviewGradeRequest(@ModelAttribute("gradeRequest") GradeRequest gradeRequest) {
        if ("approved".equals(gradeRequest.getStatus())) {
            gradeRequestService.approveRequest(gradeRequest.getRequestId());
        } else if ("rejected".equals(gradeRequest.getStatus())) {
            gradeRequestService.rejectRequest(gradeRequest.getRequestId());
        }
        return "success";
    }

    /**
     * 跳转到教师审核申请请求页面
     * @return 页面视图
     */
    @GetMapping("/teacher")
    public String toTeacherReviewRequests(){
        return "teacherReviewRequests";
    }
}
